package com.xiantm.study.thread.towparsetermination.q5;


public class Service {
    static Thread thread = null;
    static volatile boolean cancel = false;
    public static void service() {
        if (thread!=null && thread.isAlive()){
            System.out.println("is running.");
            return;
        }
        cancel  = false;
        thread = new Thread(()->{
          System.out.print("service");
          for (int i = 0; i < 50; i++) {
              if (!cancel) {
                  System.out.print(".");
                  try {
                      Thread.sleep(100);
                  } catch (InterruptedException e) {
                      break;
                  }
              }
          }
          System.out.println("done.");
      });
        thread.start();
    }

    public static void cancel() {
        System.out.println("cancel");
        cancel = true;
       thread.interrupt();
    }

}
